package JZ.JZ12;

/**
 * JZ12 数值的整数次方
 * 题目描述
 * 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
 * 保证base和exponent不同时为0
 *
 * a^b = (a^(b/2))2 = ( ( a^ ( (b/2) /2 ) ) 2 )2
 * @author corn
 * @create 2020-07-23-16:18
 */
public class Power {
    public static void main(String[] args) {
        double b = Solution.power(2,3);
        System.out.println(b);
    }
}
class Solution {
    public static double power(double base, int exponent) {
        if(exponent < 0){
            base = 1/base;
            exponent = -exponent;
        }
        double result = getPower(base, exponent);
        return result;
    }
    public static double getPower(double base,int exponent){
        if (exponent == 0) {
            return 1;
        }
        if (exponent == 1) {
            return base;
        }
        double ans = getPower(base, exponent >> 1);
        ans *= ans;
        if ((exponent & 1) == 1) {
            ans *= base;
        }
        return ans;
    }
}
